`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Module: AUDIO_TUNE                                                           //
// Author: Zhiyuan Lin                                                          //
// Date: 3/30/2014                                                              //
// Description: This module describe the tune No.2.                             //
// Detail: Currently it is "Bella ciao", an Italian folk                        //
//////////////////////////////////////////////////////////////////////////////////
module AUDIO_TUNE2(speaker, clk_div, clk_4Hz, tone);
	 output speaker;
	 output reg [4:0] tone;
	 output reg clk_4Hz;
	 input  clk_div;
	 reg  [23:0] counter_4Hz, counter_6MHz;
	 reg  [13:0] count, origin;
	 reg  clk_6MHz;
    reg  [7:0]  playback_length;
    reg speaker_temp;

assign speaker = (origin != 0) ? speaker_temp : 1'b1;	 
	 
always @(posedge clk_div) begin   
  if(counter_6MHz == 4) begin
    counter_6MHz = 0;
    clk_6MHz = ~clk_6MHz;
  end
  else begin
    counter_6MHz = counter_6MHz + 24'b1;
  end
end

always @(posedge clk_div) begin       
  if(counter_4Hz == 3125000) begin	
    counter_4Hz = 0;
    clk_4Hz = ~clk_4Hz;
  end
  else begin
    counter_4Hz = counter_4Hz + 24'b1;
  end
end

always @(posedge clk_6MHz) begin
  if(count == 16383) begin //16383 = 11111111111111
    count = origin;
    speaker_temp = ~speaker_temp;
  end
  else begin
    count = count + 14'b1;
  end
end

always @(posedge clk_4Hz) begin
  case(tone)
    'd0 : origin = 'd0;
    'd1 : origin = 'd4916; //low1 (middle C)
	 'd2 : origin = 'd5560; //low1#
    'd3 : origin = 'd6168; //low2
	 'd4 : origin = 'd6740; //low2#
    'd5 : origin = 'd7281; //low3
    'd6 : origin = 'd7791; //low4
	 'd7 : origin = 'd8275; //low4#
    'd8 : origin = 'd8730; //low5
	 'd9 : origin = 'd9159; //low5#
    'd10 : origin = 'd9565; //low6
	 'd11 : origin = 'd9948; //low6#
    'd12 : origin = 'd10310; //low7
    'd13 : origin = 'd10647; //middle1
	 'd14 : origin = 'd10972; //middle1#
    'd15 : origin = 'd11272; //middle2
	 'd16 : origin = 'd11562; //middle2#
    'd17 : origin = 'd11831; //middle3
    'd18 : origin = 'd12087; //middle4
	 'd19 : origin = 'd12329; //middle4#
    'd20 : origin = 'd12556; //middle5
	 'd21 : origin = 'd12771; //middle5#
    'd22 : origin = 'd12974; //middle6
	 'd23 : origin = 'd13165; //middle6# 
    'd24 : origin = 'd13346; //middle7
    'd25 : origin = 'd13516; //high1
	 'd26 : origin = 'd13677; //high1#	 
    'd27 : origin = 'd13829; //high2
	 'd28 : origin = 'd13972; //high2#
    'd29 : origin = 'd14108; //high3
    'd30 : origin = 'd14235; //high4
	 'd31 : origin = 'd14356; //high4#
    'd32 : origin = 'd14470; //high5
	 'd33 : origin = 'd14577; //high5#
    'd34 : origin = 'd14678; //high6
	 'd35 : origin = 'd14774; //high6#
    'd36 : origin = 'd14864; //high7
    default : origin = 'd0;
  endcase                       
end

always @(posedge clk_4Hz) begin
  if(playback_length == 64)
    playback_length = 0;
  else
    playback_length = playback_length + 8'b1;
  case(playback_length)
  0: tone = 15; // middle2
  1: tone = 17; //middle3
  2: tone = 18; //middle4, 5clock
  3: tone = 18;
  4: tone = 18;
  5: tone = 18;
  6: tone = 18;
  7: tone = 22; //middle6
  8: tone = 20; //middle5
  9: tone = 18; //middle4
  10: tone = 22; //middle6
  11: tone = 17; //middle3, 5clock
  12: tone = 17;
  13: tone = 17;
  14: tone = 17;
  15: tone = 17;
  16: tone = 15; //middle2
  17: tone = 13; //middle1
  18: tone = 12; //low7, 2clock
  19: tone = 12;
  20: tone = 17; //middle3, 2clock
  21: tone = 17;
  22: tone = 12; //low7, 2clock
  23: tone = 12;
  24: tone = 13; //middle1, 2clock
  25: tone = 13;
  26: tone = 10; //low6, 5clock
  27: tone = 10;
  28: tone = 10;
  29: tone = 10;
  30: tone = 0;
  31: tone = 0; //mute for 0.5s
  32: tone = 5; //low3
  33: tone = 10; //low6
  34: tone = 12; //low7
  35: tone = 13; //middle1
  36: tone = 10; //low6, 4clock
  37: tone = 10;
  38: tone = 10;
  39: tone = 10;
  40: tone = 5; //low3
  41: tone = 10; //low6
  42: tone = 12; //low7
  43: tone = 13; //middle1
  44: tone = 10; //low6, 4clock
  45: tone = 10;
  46: tone = 10;
  47: tone = 10;
  48: tone = 5; //low3
  49: tone = 10; //low6
  50: tone = 12; //low7
  51: tone = 13; //middle1, 2clock
  52: tone = 13;
  53: tone = 12; //low7
  54: tone = 10; //low6
  55: tone = 13; //middle1, 2clock
  56: tone = 13;
  57: tone = 12; //low7
  58: tone = 10; //low6
  59: tone = 17; //middle3
  60: tone = 0;
  61: tone = 17; //middle3
  62: tone = 0;
  63: tone = 17; //middle3
  default: tone = 0;
  endcase
end
endmodule